package com.huawei.order.service.impl;

import com.huawei.basic.util.LoginContext;
import com.huawei.basic.util.PageList;
import com.huawei.order.domain.OrderPetAcquisition;
import com.huawei.order.dto.OrderDto;
import com.huawei.order.mapper.OrderPetAcquisitionMapper;
import com.huawei.order.query.OrderPetAcquisitionQuery;
import com.huawei.order.service.IOrderPetAcquisitionService;
import com.huawei.basic.service.impl.BaseServiceImpl;
import com.huawei.org.domain.Employee;
import com.huawei.org.domain.Shop;
import com.huawei.org.mapper.EmployeeMapper;
import com.huawei.org.mapper.ShopMapper;
import com.huawei.pet.domain.SearchMasterMsg;
import com.huawei.pet.mapper.PetMapper;
import com.huawei.pet.mapper.SearchMasterMsgMapper;
import com.huawei.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;


    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if (employee.getShop_id() != null){//店员
            query.setShopId(employee.getShop_id());
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if (shop.getAdmin_id()!=employee.getId()){
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        Long orderId = orderDto.getId();
        Integer payType = orderDto.getPayType();
        BigDecimal money = orderDto.getMoney();

        //1.修改订单：state = 1，money，payType
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);
        orderPetAcquisitionMapper.update(order);

        //2.修改寻主消息：state=2，note
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("处理完成，交易成功");
        searchMasterMsgMapper.update(msg);

        //宠物
        //修改一个成本价 = money

        //@TODO - 支付模块
    }

    /**
     * 删除=取消订单
     * @param id
     * @return
     */
    @Override
    public void del(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //将寻主消息中的shop_id置空-  寻主池
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //删除宠物 - 没有成功收购
        petMapper.remove(order.getPetId());
        //删除订单
        super.del(id);
    }
}
